<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Question_model short summary.
 *
 * Question_model description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Question_model extends CI_Model
{
    public $question_id;
    public $q_contents;
    public $a_contents;
    public $weight;
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    //插入新问题
    public function inset_question($questionModel)
    {
        return $this->db->insert('question',$questionModel);
    }

    //由问题id 获取问题
    public function get_question_by_id($questionid)
    {
        return $this->db->select()->from('question')->where(array('question_id'=>$questionid))->get()->row();
    }

    //获取所有问题
    public function get_allquestion()
    {
        return $this->db->select()->from('question')->get()->result();
    }



    //更新问题
    public function edit_question($questionModel)
    {
        $this->db->update('question',$questionModel,array('question_id'=>$questionModel->question_id));
        return $this->db->affected_rows();
    }

    public function set_question_weight($questionModel)
    {
        return $this->db->query("update question set weight=".$questionModel->weight." where question_id=".$questionModel->question_id);
    }

    //删除问题
    public function del_question_byid($question_id)
    {
        return $this->db->delete('question',array('question_id'=>$question_id));
        //return $this->db->affected_rows();
    }

    //批量删除问题
    public function batchdel_question($question_ids)
    {
        $this->db->trans_begin();
        foreach($question_ids as $questioniditem)
        {
            $this->db->delete('question',array('question_id'=>$questioniditem));
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }

    public function update_weight($questionModel)
    {
        return $this->db->query("update question set weight=".$questionModel->weight." where question=".$questionModel->tid);
    }


    /**
     * @param mixed $cpage
     * @param mixed $pagesize
     * @param mixed $files
     * @param mixed $tabel
     * @param mixed $order
     */
    public function get_page($cpage,$pagesize,$files,$tabel,$order,$temp_id,$where)
    {
        $sql="SELECT $files FROM $tabel a JOIN (select $temp_id from $tabel $where $order limit ".($cpage-1)*$pagesize.", $pagesize) b ON a.$temp_id = b.$temp_id ";
        $sql_count="select count('$temp_id') as _count from $tabel $where";
        $data['objlist']=$this->db->query($sql)->result();
        $data['count']=$this->db->query($sql_count)->result();
        return $data;
    }
}